package org.klaoye.musicplayer;

import android.Manifest;
import android.annotation.SuppressLint;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.Toast;

import org.klaoye.musicplayer.bean.ChangeView;
import org.klaoye.musicplayer.bean.FileSelector;
import org.klaoye.musicplayer.bean.MessageDriver;

import java.io.File;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, MessageDriver, ChangeView {

    public File currentFile;
    FragmentTransaction ft;
    Fragment playerFragment;
    Fragment channelFragment;
    RadioButton player, channel;
    Bundle bundle;

    public MainActivity() {
        playerFragment = new PlayerFragment();
        channelFragment = new ChannelFragment();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ft = getSupportFragmentManager().beginTransaction();
        player = findViewById(R.id.btn_player);
        channel = findViewById(R.id.btn_channel);

        player.setOnClickListener(this);
        channel.setOnClickListener(this);

        channel.setChecked(true);
        replaceFregment(channel);

        requestPermissions();//测试权限
        //startService(new Intent(this,MusicService.class));
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    @Override
    public void onClick(View v) {
        ft = getSupportFragmentManager().beginTransaction();
        replaceFregment(v);
    }

    private void replaceFregment(View v) {
        switch (v.getId()) {
            case R.id.btn_player:
                ft.replace(R.id.fragment_frame, playerFragment);
                Toast.makeText(this, R.string.player, Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_channel:
                ft.replace(R.id.fragment_frame, channelFragment);
                Toast.makeText(this, R.string.channel, Toast.LENGTH_SHORT).show();
                break;
            default:
                ft.replace(R.id.fragment_frame, playerFragment);
                break;
        }
        ft.commit();
    }

    private void requestPermissions() {
        // 如果未获得外部存储读写权限，则申请
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            // 申请权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
        }
    }

    //申请权限
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 1:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // 允许权限，将文件的路径写在下面的方法里；
                    Toast.makeText(this, "允许权限", Toast.LENGTH_SHORT).show();
                } else {
                    // 拒绝权限
                    Toast.makeText(this, "拒绝权限", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    @Override
    public void getMessage(Bundle message) {
        this.bundle = message;
        Log.i("getMessage", bundle.toString());
    }

    @Override
    public void changeButton(int flag) {
        switch (flag) {
            case 1:
                player.setChecked(true);
                break;
            case 2:
                channel.setChecked(true);
                break;
            case 3:
                break;
            default:
                break;
        }
    }
}